#!/usr/bin/env node

/**
 * exports 字段测试脚本
 * 演示 exports 字段的作用和效果
 */

const fs = require('fs');
const path = require('path');

console.log('🧪 exports 字段测试\n');

// 读取当前配置
const packageJsonPath = path.join(__dirname, '../packages/ui/package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));

console.log('📋 当前 exports 配置:');
console.log(JSON.stringify(packageJson.exports, null, 2));

// 测试不同的导入方式
const testCases = [
  {
    name: '主入口导入',
    import: '@repo/ui',
    description: '导入所有组件',
    expected: '应该能导入 Button, Card, Code',
    status: '✅ 支持'
  },
  {
    name: '子路径导入 - Button',
    import: '@repo/ui/button',
    description: '只导入 Button 组件',
    expected: '应该能导入 Button，不包含其他组件',
    status: '✅ 支持'
  },
  {
    name: '子路径导入 - Card',
    import: '@repo/ui/card',
    description: '只导入 Card 组件',
    expected: '应该能导入 Card，不包含其他组件',
    status: '✅ 支持'
  },
  {
    name: '子路径导入 - Code',
    import: '@repo/ui/code',
    description: '只导入 Code 组件',
    expected: '应该能导入 Code，不包含其他组件',
    status: '✅ 支持'
  },
  {
    name: '私有文件访问 - src',
    import: '@repo/ui/src/button',
    description: '尝试访问源码文件',
    expected: '应该被拒绝（私有文件）',
    status: '❌ 拒绝'
  },
  {
    name: '私有文件访问 - dist',
    import: '@repo/ui/dist/button',
    description: '尝试直接访问 dist 文件',
    expected: '应该被拒绝（私有文件）',
    status: '❌ 拒绝'
  }
];

console.log('\n🔍 导入方式测试:');
testCases.forEach((testCase, index) => {
  console.log(`\n${index + 1}. ${testCase.name}`);
  console.log(`   导入: ${testCase.import}`);
  console.log(`   描述: ${testCase.description}`);
  console.log(`   预期: ${testCase.expected}`);
  console.log(`   状态: ${testCase.status}`);
});

// 检查构建输出
console.log('\n📁 构建输出文件:');
const distPath = path.join(__dirname, '../packages/ui/dist');
if (fs.existsSync(distPath)) {
  const files = fs.readdirSync(distPath);
  const jsFiles = files.filter(file => file.endsWith('.js'));
  
  jsFiles.forEach(file => {
    const filePath = path.join(distPath, file);
    const stats = fs.statSync(filePath);
    const sizeKB = (stats.size / 1024).toFixed(2);
    console.log(`  ${file}: ${sizeKB} KB`);
  });
}

// 演示 exports 的安全性
console.log('\n🔒 exports 安全性演示:');

const securityExamples = [
  {
    scenario: '没有 exports 字段',
    description: '用户可以访问包内的任何文件',
    examples: [
      'import { Button } from "@repo/ui/src/button"',
      'import { Button } from "@repo/ui/dist/button"',
      'import React from "@repo/ui/node_modules/react"'
    ],
    risk: '高风险 - 暴露内部实现'
  },
  {
    scenario: '有 exports 字段（当前）',
    description: '只能访问 exports 中定义的文件',
    examples: [
      'import { Button } from "@repo/ui/button" ✅',
      'import { Button } from "@repo/ui/src/button" ❌',
      'import { Button } from "@repo/ui/dist/button" ❌'
    ],
    risk: '低风险 - 只暴露公共 API'
  }
];

securityExamples.forEach((example, index) => {
  console.log(`\n${index + 1}. ${example.scenario}`);
  console.log(`   描述: ${example.description}`);
  console.log(`   示例:`);
  example.examples.forEach(exp => {
    console.log(`     ${exp}`);
  });
  console.log(`   风险: ${example.risk}`);
});

// 演示不同配置的效果
console.log('\n⚙️ 不同配置效果对比:');

const configs = [
  {
    name: '只有主入口',
    exports: {
      ".": "./dist/index.js"
    },
    usage: [
      'import { Button, Card, Code } from "@repo/ui" ✅',
      'import { Button } from "@repo/ui/button" ❌'
    ],
    pros: ['配置简单', '构建快速'],
    cons: ['不支持按需导入', 'Tree-shaking 效果有限']
  },
  {
    name: '多入口（当前）',
    exports: {
      ".": "./dist/index.js",
      "./button": "./dist/button.js",
      "./card": "./dist/card.js",
      "./code": "./dist/code.js"
    },
    usage: [
      'import { Button, Card, Code } from "@repo/ui" ✅',
      'import { Button } from "@repo/ui/button" ✅'
    ],
    pros: ['支持按需导入', '更好的 Tree-shaking', '灵活的 API'],
    cons: ['配置稍复杂', '构建时间稍长']
  }
];

configs.forEach((config, index) => {
  console.log(`\n${index + 1}. ${config.name}`);
  console.log(`   配置:`);
  console.log(JSON.stringify(config.exports, null, 4));
  console.log(`   使用方式:`);
  config.usage.forEach(usage => {
    console.log(`     ${usage}`);
  });
  console.log(`   优点: ${config.pros.join(', ')}`);
  console.log(`   缺点: ${config.cons.join(', ')}`);
});

// 实际使用建议
console.log('\n💡 实际使用建议:');

console.log('\n🎯 开发阶段:');
console.log('  - 使用主入口导入，方便调试');
console.log('  - import { Button, Card, Code } from "@repo/ui"');

console.log('\n🚀 生产阶段:');
console.log('  - 使用子路径导入，优化打包体积');
console.log('  - import { Button } from "@repo/ui/button"');

console.log('\n🔧 配置建议:');
console.log('  - 保持当前的多入口配置');
console.log('  - 提供灵活的导入方式');
console.log('  - 确保 API 安全性');

console.log('\n🎯 测试完成！');
console.log('\n📚 更多信息:');
console.log('  - 详细说明: docs/exports-explanation.md');
console.log('  - 多入口配置: docs/entry-points-explanation.md'); 